'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  /**
   * 首页入口（可选）
   * GET /
   */
  async index() {
    const { ctx } = this;
    ctx.body = {
      success: true,
      message: 'Welcome to Trave Project API',
      version: '1.0.0'
    };
  }

  async getHotCities() {
    const { ctx, service } = this;
    const { limit = 6 } = ctx.query;

    try {
      const cities = await service.home.getHotCities(parseInt(limit));
      ctx.body = {
        success: true,
        data: cities
      };
    } catch (error) {
      ctx.logger.error('获取热门城市失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '获取热门城市失败: ' + error.message
      };
    }
  }

  async getHotNotes() {
    const { ctx, service } = this;
    const { limit = 6 } = ctx.query;

    try {
      const notes = await service.home.getHotNotes(parseInt(limit));
      ctx.body = {
        success: true,
        data: notes
      };
    } catch (error) {
      ctx.logger.error('获取热门游记失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '获取热门游记失败: ' + error.message
      };
    }
  }

  async getHotPlans() {
    const { ctx, service } = this;
    const { limit = 6 } = ctx.query;

    try {
      const plans = await service.home.getHotPlans(parseInt(limit));
      ctx.body = {
        success: true,
        data: plans
      };
    } catch (error) {
      ctx.logger.error('获取热门旅行计划失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '获取热门旅行计划失败: ' + error.message
      };
    }
  }

  async getFeaturedCommunity() {
    const { ctx, service } = this;
    const { limit = 4 } = ctx.query;

    try {
      const posts = await service.home.getFeaturedCommunity(parseInt(limit));
      ctx.body = {
        success: true,
        data: posts
      };
    } catch (error) {
      ctx.logger.error('获取社区精选失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '获取社区精选失败: ' + error.message
      };
    }
  }
}

module.exports = HomeController;
